home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / COMM / PORTTEST.ARJ / TALK.PAS < prev   
Pascal/Delphi Source File  |  1991-09-16  |  6KB  |  179 lines

  1. {\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  2. The contents of this file are not copyrighted.  Use it any way you want to.
  3.  
  4.         File : TALK.PAS
  5.         Type : Mainline
  6.     Language : TP6
  7.     Revision : 1.0
  8.       Author : Robert C. Henningsgard
  9.         Date : 091691
  10.  Description : COM port test talker.
  11.  
  12.                Uses the Asynch Plus communications library
  13.                from Blaise Computing Inc, 2560 Ninth Street, Suite 316,
  14.                Berkeley, CA 94710 (415-540-5441).
  15.  
  16. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\}
  17. uses DOS,CRT,TURBOEXT,UNIT_A0,UNIT_A1;
  18.  
  19. const
  20.   ProgramName = 'TALK - RS-232C Port Exerciser';
  21.   ProgramRevision = '1.0';
  22.   Copyright =
  23.     'Freeware - Committed to the public domain 1991 by Rob Henningsgard.';
  24.   TimeOutMs = 5000;
  25.  
  26. const
  27.   InQSize = 256;
  28.   OutQSize = 256;
  29.   BufferOverhead = 4;
  30.   MinPort = 1;
  31.   MaxPort = 4;
  32.  
  33. type
  34.   Buf = array[1..InQSize+OutQSize+BufferOverhead] of byte;
  35.   BufferArray = array[MinPort..MaxPort] of Buf;
  36.  
  37. var
  38.   CommunicationsInitialized : array[MinPort..MaxPort] of boolean;
  39.   InterCharacterDelay : array[MinPort..MaxPort] of integer;
  40.   Buffer : BufferArray;
  41.  
  42. function COMAddress(I : integer) : word;
  43. begin
  44.   if (I >= 1) and (I <= 4)
  45.     then COMAddress := memw[0:($400 + 2*pred(I))]
  46.     else COMAddress := 0;
  47. end;
  48.  
  49. procedure Async_Send(PortNumber : word;C : char);
  50. begin
  51.   while (__WrtChA1(PortNumber,C) = _Out_Q_Full) and
  52.         (not keypressed) do begin                   { Queue is full }
  53.     asm         { This calls the DOS idle interrupt.  It's a good idea to }
  54.       int $28   { call it whenever you're going to cool your heels for    }
  55.     end;        { a while.  It lets stuff like networks, printers, and    }
  56.     delay(1);   { other background processes get a chance at the CPU.     }
  57.   end;
  58. end; { Async_Send }
  59.  
  60. function AsyncReceive(PortNumber : word;var C : char) : boolean;
  61. var
  62.   W,InQSize,PortStatus : word;
  63. begin
  64.   W := __RdChA1(PortNumber,C,InQSize,PortStatus);
  65.   AsyncReceive := (W = 0);
  66. end; { AsyncReceive}
  67.  
  68. procedure Finish_Communications(PortNumber : word);
  69. {
  70. Close port and drop DTR
  71. }
  72. var
  73.   W : word;
  74. begin { Finish_Communications }
  75.   if CommunicationsInitialized[PortNumber] then begin
  76.     W := __CloseA1(PortNumber);
  77.     CommunicationsInitialized[PortNumber] := false;
  78.   end;
  79. end; { Finish_Communications }
  80.  
  81. function InitCommunications(PortNumber,BaudRate,Delay : integer) : boolean;
  82. const
  83.   IntLevel = 0;
  84.   PortAds = 0;
  85.   NoParity = 0;
  86.   EightDataBits = 3;
  87.   OneStopBit = 0;
  88.   NoXONXOFF = 0;
  89.   NoBit7Trimming = 0;
  90.   NoBit7Forcing = 0;
  91.   DoNotRequireCTS = 0;
  92.  
  93. var
  94.   W : word;
  95.   BaudVar : integer;
  96. begin { InitCommunications }
  97.   InitCommunications := false;
  98.   if (BaudRate mod 150 <> 0) then exit; { illegal }
  99.   BaudVar := 8;
  100.   while (BaudRate < 19200) do begin { set up Asynch Plus baud number   }
  101.     BaudRate := BaudRate * 2;       { where 8=19200, 7=9600, 6=4800... }
  102.     dec(BaudVar);
  103.   end;
  104.   if (PortNumber in[MinPort..MaxPort]) and __LCOMOKA1 then begin
  105.     if (__OpenA1(PortNumber,InQSize,OutQSize,IntLevel,
  106.                  PortAds,@Buffer[PortNumber]) <> 0) then exit;
  107.     if (__SetOpA1(PortNumber,1,BaudVar) <> 0) then exit;
  108.     if (__SetOpA1(PortNumber,2,NoParity) <> 0) then exit;
  109.     if (__SetOpA1(PortNumber,3,EightDataBits) <> 0) then exit;
  110.     if (__SetOpA1(PortNumber,4,OneStopBit) <> 0) then exit;
  111.     if (__SetOpA1(PortNumber,5,NoXONXOFF) <> 0) then exit;
  112.     if (__SetOpA1(PortNumber,6,NoXONXOFF) <> 0) then exit;
  113.     if (__SetOpA1(PortNumber,9,DoNotRequireCTS) <> 0) then exit;
  114.     InitCommunications := true;
  115.     InterCharacterDelay[PortNumber] := Delay;
  116.     CommunicationsInitialized[PortNumber] := true;
  117.   end;
  118. end; { InitCommunications }
  119.  
  120. var
  121.   I : integer;
  122.   Port,Baud : integer;
  123.   S : string;
  124.   C : char;
  125.  
  126. begin { Listen }
  127.   writeln(ProgramName,' Rev ',ProgramRevision);
  128.   writeln(Copyright);
  129.   writeln;
  130.   if (paramcount < 2) then begin
  131.     writeln('TALK  PORT  BAUD');
  132.     writeln('      |     |');
  133.     writeln('      |     +-- the baud rate to listen at');
  134.     writeln('      +-- the port number to listen to');
  135.     halt;
  136.   end;
  137.   S := paramstr(1);
  138.   val(S,Port,I);
  139.   S := paramstr(2);
  140.   val(S,Baud,I);
  141.   if not ((Port > 0) and (Port < 4)) then begin
  142.     writeln('Illegal Port number ',Port,'.  Must be in the range 1..4.');
  143.     halt;
  144.   end;
  145.   if (COMAddress(Port) = 0) then begin
  146.     writeln('There is no COM',Port,' installed in this machine.');
  147.     halt;
  148.   end;
  149.   if not ((Baud > 74) and (Baud < 19201)) then begin
  150.     writeln('Illegal Baud rate ',Baud,'.  Must be in the range 75..19200.');
  151.     halt;
  152.   end;
  153.   clrscr;
  154.   writeln(ProgramName,' Rev ',ProgramRevision);
  155.   writeln(Copyright);
  156.   writeln;
  157.   writeln('Talking to port ',Port,' at ',Baud,' baud.  Press any key to quit.');
  158.   window(1,6,80,22);
  159.   clrscr;
  160.   for I := MinPort to MaxPort do CommunicationsInitialized[I] := false;
  161.   if not InitCommunications(Port,Baud,0) then begin
  162.     writeln('Unable to initialize the port.');
  163.     halt;
  164.   end;
  165.   I := 1;
  166.   S := ProgramName + ' ' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'+#13+#10;
  167.   while not keypressed do begin
  168.     if (I > length(S)) then I := 1;
  169.     Async_Send(Port,S[I]);
  170.     write(S[I]);
  171.     inc(I);
  172.   end;
  173.   while keypressed do C := readkey;
  174.   Finish_Communications(Port);
  175.   window(1,1,80,24);
  176.   gotoxy(1,23);
  177.   writeln(ProgramName,' ended.');
  178. end. { Listen }
  179.